package com.example.sxt_practise.thread;


/**
 * @author gaofeng
 * @date 2022年05月26日 20:57
 */
public class UnSafeThreadTest {
    public static void main(String[] args) {

        Account account = new Account(100, "基金") ;

        Drawing you = new Drawing(account, 50, "男") ;
        Drawing youGirlFriend = new Drawing(account, 100, "女") ;

        you.start();
        youGirlFriend.start();
    }
}

class Account {
    private int money ;
    private String name ;

    public Account(int accountNum, String name) {
        this.money = accountNum;
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Drawing extends Thread {
    Account account ;
    int drawingMoney ;
    int nowMoney ;

    public Drawing(Account account, int drawingMoney, String nowMoney) {
        super(nowMoney);
        this.account = account ;
         this.drawingMoney = drawingMoney ;
    }

    @Override
    public void run() {
        synchronized (account) {
            if (account.getMoney() - drawingMoney < 0) {
                System.out.println(Thread.currentThread().getName() + "钱不够");
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.setMoney(account.getMoney() - drawingMoney);

            nowMoney += drawingMoney ;

            System.out.println(account.getName() + "账户余额:" + account.getMoney());
            System.out.println(account.getName() + "手里有:" + nowMoney);
        }

    }
}